package com.sxbbc.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.RestResponse;
import com.sxbbc.api.common.AppScanMapping;
import com.sxbbc.api.service.IRegisterService;
import com.sxbbc.common.basics.enums.LoginChannelEnums;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.FunctionUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.LoginRegisterVo;
import com.sxbbc.common.core.entity.jz.AppVersion;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.entity.jz.CCustomerAuthen;
import com.sxbbc.common.core.entity.jz.SysAgreement;
import com.sxbbc.common.core.service.IAppVersionService;
import com.sxbbc.common.core.service.ICCustomerAuthenService;
import com.sxbbc.common.core.service.ICCustomerService;
import com.sxbbc.common.core.service.ISysAgreementService;

/**
 * app扫码操作controller
 * @author xiaoky
 * @date 2019-08-15
 */
@Controller

public class AppScanController {
	@Autowired
	private IRegisterService registerService;
	@Autowired
	private ISysAgreementService sysAgreementService;
	@Autowired
	private IAppVersionService appVersionService;
	@Autowired
	private ICCustomerService icCustomerService;
	@Autowired
	private ICCustomerAuthenService icCustomerAuthenService;
	
	/**
	 * app扫码显示页面
	 * @param inviteCode 推荐码
	 * @return org.springframework.web.servlet.ModelAndView
	 * @author xiaoky
	 */
	@RequestMapping(value = AppScanMapping.SHOW_SHARE_APP)
	public ModelAndView showShareApp(String inviteCode) {
		CalcuUtils calc = CalcuUtils.getInstance();
		ModelAndView view = new ModelAndView(AppScanMapping.SHOW_SHARE_APP);
		Integer type = 2;
        SysAgreement agree = sysAgreementService.getById(type);
		view.addObject("inviteCode", inviteCode);
		view.addObject("agree", agree);
		//推荐人信息
		String name = "";
		CCustomer customer = icCustomerService.getByInviteCode(inviteCode);
		if(customer != null) {
			name = FunctionUtils.replay(customer.getPhone());
			if(calc.isEquals(customer.getAuthen(), StaticUtils.STATUS_YES)) {
				QueryWrapper<CCustomerAuthen> queryWrapper = new QueryWrapper<CCustomerAuthen>();
				queryWrapper.eq("cid", customer.getId());
				CCustomerAuthen authen = icCustomerAuthenService.getOne(queryWrapper);
				if(authen != null) {
					name = authen.getRealName();
				}
			}
		}
		view.addObject("name",name);
		return view;
	}
	
	/**
	 * 处理app扫码注册
	 * @param vo 注册所需参数
	 * @return com.function.common.RestResponse
	 * @author xiaoky
	 */
	@RequestMapping(value = AppScanMapping.HANDLE_SCAN_REGISTER)
	@ResponseBody
	public RestResponse handleShareRegister (LoginRegisterVo vo) {
		// 设置默认值
		// 默认注册渠道和注册类型
		vo.setChannel(LoginChannelEnums.APP.getChannel());
		vo.setType(StaticUtils.PHONE_LOGIN);
		return registerService.handleRegister(vo, null);
	}
	
	/**
	 * 协议页面
	 * @param type 区分什么协议
	 * @return org.springframework.web.servlet.ModelAndView
	 * @author xiaoky
	 */
	@RequestMapping(value = AppScanMapping.SHOW_AGREE)
	public ModelAndView showAgree(Integer type) {
		ModelAndView view = new ModelAndView(AppScanMapping.SHOW_AGREE);
		SysAgreement agree = sysAgreementService.getById(type);
		view.addObject("agree", agree);
		return view;
	}
	
	/**
	 * App下载页面
	 * @return org.springframework.web.servlet.ModelAndView
	 * @author xiaoky
	 */
	@RequestMapping(value = AppScanMapping.SHOW_DOWNLOAD)
	public ModelAndView showDownload() {
		ModelAndView view = new ModelAndView(AppScanMapping.SHOW_DOWNLOAD);
		AppVersion appVersion = appVersionService.getOne(new QueryWrapper<AppVersion>());
        if (appVersion == null) {
            throw new ThrowJsonException("未获取到最新版本号信息");
        }
        view.addObject("version", appVersion);
		return view;
	}
}
